//配置所有的路由文件，然后导入到main.js里面
import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/views/Home";
import FoodDetail from "@/views/FoodDetail";
import Login from "@/views/Login";
import ChooseFood from "@/views/ChooseFood";
import My from "@/views/My";
import Category from "@/views/Category";
import Order from "@/views/Order";
import Register from "@/views/Register";
import ShopCartList from "@/views/ShopCartList";
import AddressInfoList from "@/views/AddressInfoList";
import AddAddressInfo from "@/views/AddAddressInfo";
import ChangeAddress from "@/views/ChangeAddress";
import store from "@/store";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      //初始化/的初始页面
      path: "/",
      redirect: {
        name: "ChooseFood",
      },
    },
    {
      path: "/Home",
      name: "Home",
      component: Home,
      //设置主页面下的子页面的路由
      children: [
        {
          path: "ChooseFood",
          name: "ChooseFood",
          component: ChooseFood,
        },
        {
          path: "My",
          name: "My",
          component: My,
        },
        {
          path: "Category",
          name: "Category",
          component: Category,
        },
        {
          path: "Order",
          name: "Order",
          component: Order,
        },
      ],
    },
    {
      path: "/FoodDetail/:id",
      name: "FoodDetail",
      component: FoodDetail,
    },
    {
      path: "/Login",
      name: "Login",
      component: Login,
    },
    {
      path: "/Register",
      name: "Register",
      component: Register,
    },
    {
      path: "/ShopCartList",
      name: "ShopCartList",
      component: ShopCartList,
      //通过meta标签附加了网页的基本信息
      //同时要注意requiredAuth 我们假设就是要登录哟吼才可以访问
      meta: {
        requiredAuth: true,
      },
    },
    {
      path: "/AddressInfoList",
      name: "AddressInfoList",
      component: AddressInfoList,
      meta: {
        requiredAuth: true,
      },
    },
    {
      path: "/AddAddressInfo",
      name: "AddAddressInfo",
      component: AddAddressInfo,
      meta: {
        requiredAuth: true,
      },
    },
    {
      path: "/ChangeAddress/:id",
      name: "ChangeAddress",
      component: ChangeAddress,
      meta: {
        requiredAuth: true,
      },
    },
  ],
});

/**
 * 导航守卫
 * 1.前置导航守卫
 * 2.后置导航守卫
 */
//如果要在跳转某个页面之前做拦截，可以命令与征服和前置导航守卫

router.beforeEach((to, from, next) => {
  //to代表你要去哪
  //from代表你从哪里来
  //next()决定放心
  console.log("我是beforeEach");
  console.log(to);
  if (to.meta.requiredAuth == true) {
    //说明你要去的这个页面要登录
    if (store.getters.loginUserInfo) {
      //说明已经了
      next();
    } else {
      //说明， 没有登录
      next({
        name: "Login",
      });
    }
  } else {
    //说明器的这个页面不用登录
    next();
  }
});

// router.afterEach((to, from, next) => {
//     //to代表你要去哪
//     //from代表你从哪里来
//     //next()决定放心
//     console.log("我是beforeEach");
//     next();
//   });
export default router;
